package com.zmh.service.timer;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.zmh.common.DateUtil;
import com.zmh.dao.MessageInfoDao;
import com.zmh.model.book.BookFav;
import com.zmh.model.wechat.httpClient.MessageInfo;
import com.zmh.service.httpclient.BookLink;
import com.zmh.service.httpclient.Dwz;
import com.zmh.service.httpclient.WeChat;
import com.zmh.service.wechat.MessageInfoService;

/** 
 * 工作日志管理类
 * @author zhoumh
 * @version 创建时间：2017年10月31日 下午4:16:22 
 * @Description 
*/
@Service
@EnableScheduling
public class BookLinkTimer {
	private static Logger logger = LoggerFactory.getLogger(BookLinkTimer.class);
	@Autowired
	private BookLink bookLink;
	@Autowired
	private Dwz dwz;
	@Autowired
	private MessageInfoService messageInfoService;
	/**
	 * 获取书籍更新并保存到待发消息列表中
	 * @return: void
	 * @author: zhoumh
	 * @version: 创建时间：2017年10月31日 下午4:21:01 
	 * @throws UnsupportedEncodingException 
	 * @Description: 更新最新章节搜索链接为百度搜索链接
	 */
//	@Scheduled(cron = "13 0/5 * * * ? ")
	public void getBookUpdate() throws UnsupportedEncodingException {
		// 微信公众号ID
		String zmh = "oq8Y20dXcYxg4Pk_dI6VVtnLZM0Q";
		// 获取booklink更新书籍列表
		List<BookFav> updateBook = bookLink.getUpdateBook();
		if (updateBook==null || updateBook.size()<1) {
			logger.info("本次未发现更新！");
			return;
		}
		for (BookFav bookFav : updateBook) {
			MessageInfo messageInfo = new MessageInfo();
			String url = bookFav.getBname()+"+"+bookFav.getNstatus();
			String longurl = "https://www.so.com/s?ie=utf-8&fr=none&src=360sou_newhome&q="+url.replace(" ", "+");
			// 消息内容
			String content = DateUtil.getDateTime()+bookFav.getBname()+" 更新了最新章节 "+bookFav.getNstatus()+" 访问链接："+dwz.mrw(longurl);
			// 发送类型 微信公众号
			messageInfo.setSendmode("0");
			// 发送内容
			messageInfo.setContent(content);
			// 发送对象
			messageInfo.setOpenid(zmh);
			// 书籍类消息
			messageInfo.setType("1");
			// 消息发送时间
			messageInfo.setSendtime(DateUtil.getDateTime());
			messageInfoService.insMsg(messageInfo);
		}
		
	}
	
}
